package com.mtq.utils;

import java.util.List;

public class PageModel {

	private int pageNum = 1; // 当前选中页号
	private int pageSize = 10; // 每页多少条数据
	private int startNum = 0; // 起始号
	private int totalRow; // 共多少行
	private int totalPage; // 共多少页
	private int prePage; // 上一页
	private int nextPage; // 下一页
	private int pageNow = 0; //当前页
	private List<?> data;
	private int count;
	
	public PageModel(){
		
	}
	
	public PageModel(int pageSize) {
		this.pageSize = pageSize;
	}
	
	/**
	 * 设置总记录数
	 * @param totalRow
	 */
	public void setTotalRow(int totalRow) {
		this.totalRow = totalRow;
		handlePage();
	}
	
	/**
	 * 计算页码
	 */
	public void handlePage() {
		if (totalRow == 0) {
			totalPage = 0;
		} else {
			totalPage = totalRow / pageSize;
			if (totalRow % pageSize > 0) 
				totalPage = totalPage + 1;
			this.setCount(totalRow);
		}
		// 处理页码为合法页码，必须为大于0的整数
		if (this.pageNum <= 0) {
			pageNow = 1;
		} else {
			pageNow = this.pageNum;
		}
		// 判断当前的页码是否大于了总页数，并且要总页数大于0则定位到最后一页
		if (pageNow > this.getTotalPage() && this.getTotalPage() > 0) {
			pageNow = this.getTotalPage();
		} else if (this.getTotalPage() == 0) { // 当记录结果集为空时，表示当前没有记录，则定位到第一页
			pageNow = 1;
		}
		int temp = pageSize * (pageNow-1); // 计算起始号
		this.setStartNum(temp);
		this.pageNum = pageNow;
		this.prePage = pageNow - 1;
		this.nextPage = pageNow + 1;
	}
	
	/**
	 * 设置当前的页码
	 * @param pageNum
	 */
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	
	/**
	 * @function 拼装分页查询数据sql（只针对Oracle）
	 *           注意：调用本方法时请确保你已经执行了getContSql(),和setTotalRows()方法
	 * @author
	 * @param sql
	 * @return sqlString
	 * @date 2014年5月28日
	 */
	public String getQuerySql(String sql) {
		StringBuffer paginationSQL = new StringBuffer(" SELECT * FROM ( ");
		paginationSQL.append(" SELECT temp.* ,ROWNUM num FROM ( ");
		paginationSQL.append(sql);
		paginationSQL.append("　) temp ");
		paginationSQL.append(" ) WHERE　num > " + startNum + " and ROWNUM <= "
				+ pageSize);
		return paginationSQL.toString();
	}
	
	public String getMysqlLimit() {
		return " LIMIT " + startNum + " , " + pageSize;
	}
	
	
	public int getStartNum() {
		return startNum;
	}
	
	public void setStartNum(int startNum) {
		this.startNum = startNum;
	}
	
	public int getCount() {
		return count;
	}
	
	public void setCount(int count) {
		this.count = count;
	}
	
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	
	public int getPrePage() {
		return prePage;
	}
	
	public void setPrePage(int prePage) {
		this.prePage = prePage;
	}
	
	public int getNextPage() {
		return nextPage;
	}
	
	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}
	
	public int getPageNum() {
		return pageNum;
	}
	
	public int getPageSize() {
		return pageSize;
	}
	
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	public int getTotalRow() {
		return totalRow;
	}
	
	public int getTotalPage() {
		return totalPage;
	}
	
	public List<?> getData() {
		return data;
	}
	
	public void setData(List<?> data) {
		this.data = data;
	}
	
	
}
